package sources;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * A class representing an iCalendar.
 *
 * @author Alana Meditz
 * @author Ty Yago
 * @author Katherine Kishaba
 * @author Jimmy CUmming
 * 
 */

public class ICalendar {
	
	// Highest version number of the iCalendar specification that required to
	// interpret the iCalendar object
	private final String VERSION = "2.0";
	
	// Identifier for the product that created the iCalendar object
	private final String PRODID = "TeamKobuk//iCalendar File Creator";

	// List of properties of the iCalendar object as a whole
	private List<ICalProperty> iCalProperties = new ArrayList<ICalProperty>();
	// List of components of the iCalendar object
	private List<ICalComponent> iCalComponents = new ArrayList<ICalComponent>();
	
	/**
	 * ICalendar constructor.  Sets required properties.
	 */
	public ICalendar() {
		iCalProperties.add(new ICalProperty("VERSION", VERSION));
		iCalProperties.add(new ICalProperty("PRODID", PRODID));
	}
	
	/**
	 * Adds a ICalProperty to this ICalendar's property list.
	 * 
	 * @param name Name of new property
	 * @param value Value of new property
	 */
	public void addICalProperty(String name, String value) {
		iCalProperties.add(new ICalProperty(name, value));
	}
	
	/**
	 * Adds given ICalComponent to this ICalendar's component list.
	 * @param newComponent ICalComponent to be added to this ICalendar
	 */
	public void addComponent(ICalComponent newComponent) {
		iCalComponents.add(newComponent);
	}
	
	/**
	 * Writes this iCalendar object to the given file.
	 * 
	 * @param file File to be written in
	 */
	public void writeICalFile(File file) {
		try {
			Writer output = new BufferedWriter(new FileWriter(file));
			output.write("BEGIN:VCALENDAR\n");
			writeICalProperties(output);
			writeICalComponents(output);
			output.write("END:VCALENDAR\n");
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Helper method to writeICalFile(File).  Writes the ICalProperties of the calendar to the
	 * file.
	 * 
	 * @param output       Writer for desired destination File
	 * @throws IOException
	 */
	private void writeICalProperties(Writer output) throws IOException {
		for(ICalProperty property : iCalProperties) {
			output.write(property.getName() + ":" + property.getValue() + "\n");
		}
	}
	
	/**
	 * Helper method to writeICalFile(File).  Writes the ICalComponents and their ICalProperties to
	 * the file.
	 * 
	 * @param output       Writer for desired destination File
	 * @throws IOException
	 */
	private void writeICalComponents(Writer output) throws IOException {
		for(ICalComponent iCalComponent : iCalComponents) {
			output.write("BEGIN:" + iCalComponent.getType() + "\n");
			for(ICalProperty property : iCalComponent.getComponentProperties()) {
				output.write(property.getName() + ":" + property.getValue() + "\n");
			}
			output.write("END:" + iCalComponent.getType() + "\n");
		}
	}
	
	/**
	 * @return ICalProperties of this ICalendar
	 */
	public List<ICalProperty> getICalProperties() {
		return iCalProperties;
	}
	
	/**
	 * @return ICalComponents of this ICalendar
	 */
	public List<ICalComponent> getICalComponents() {
		return iCalComponents;
	}
}